import Vue from 'vue';

import io from 'socket.io-client';

// element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-default/index.css';
Vue.use(ElementUI);

// router
import VueRouter from 'vue-router';
Vue.use(VueRouter);

// vuex
import Vuex from 'vuex';
Vue.use(Vuex);

//页面顶部进度条
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';


// root component
import App from './App';

//vuex store
import store from './store/';


import routes from './config/router.js';

import VueAMap from 'vue-amap';
Vue.use(VueAMap);

import 'register/';
import 'mixin/';

const router = new VueRouter({
	routes
});

VueAMap.initAMapApiLoader({
	key: 'a1ec30fb18df7028613b275c8bafb776',
	plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'Geocoder']
})

router.beforeEach((to, from, next) => {

	window.scroll(0, 0);

	var userinfo = store.state.user.userinfo;

	if (!userinfo.token){
		switch(to.path) {
			case '/register':
				 next();
				break;
			case '/login':
				 next();
				break;
			case '/reminder':
				 next();
				break;	
			default:
			store.dispatch('remove_userinfo');
			next('/login');
		}
	}
	else if(userinfo.token && userinfo.status !== 0 && to.path !== '/authen') {
		next('/authen')
	}
	else {

		if (userinfo.token && (to.path === '/login' || to.path === '/register' || to.path === '/reminder')) {
			next({
				path: '/index/home'
			});
		}else{
			NProgress.start();
			next();
		}

	}
})

router.afterEach(transition => {
	NProgress.done();
});

const appAdmin = new Vue({
	el    : '#app',
	data() {
		return {};
	},
	// template: '<App/>',
	router,
	store,
	render: h => h(App)
})